require 'barby/outputter/svg_outputter'

module ItemsHelper
	def add_contained_item(name, container_div, prefix)
		link_to_function name, :class => 'add_item_to_container' do |page|
			page.insert_html :bottom, container_div, :partial => 'contained_item_form', 
				:object => populate_new_item(true), :locals => {:prefix => prefix}
		end
	end
	
	def add_more_to_container(name, container_div, prefix)
		link_to_function name, :class => 'add_item_to_container' do |page|
				page.insert_html :bottom, container_div, :partial => 'contained_item_existing', 
					:object => Item.new, :locals => {:prefix => prefix}
			end
	end
	
	def show_barcode(id)
	  Barby::Code128B.new(id).to_svg({:height => 45})
	end
	
	def create_item_attribute_map(attribute_sets)
	  attribute_sets.sort_by{|s| s.name}.sort_by {|s| 
			if (s.name == "Other Attributes")
				1
			else
				0
			end
		}.map {|set|
			if set.item_attributes.first
				if set.mutually_exclusive
					{	:name => set.name, 
						:description => set.description,
						:required => set.required,
						:mutually_exclusive => set.mutually_exclusive,
						:item_attributes => set.item_attributes
					}
				else
					set.item_attributes.map {|ia|
						{	:name => set.name, 
							:description => set.description,
							:required => set.required,
							:mutually_exclusive => set.mutually_exclusive,
							:first => (ia == set.item_attributes.first),
							:last => (ia == set.item_attributes.last),
							:item_attribute => 	{	:id => ia.id,
													:display_text => ia.display_text,
												}
						}
					}
				end
			end
		}.flatten
	end
end
